/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ImportDataFromPhone;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 *
 * @author o.rybyanskyy
 */
public class CatMap {
    
    private static String defaultCategory = "default";
    private static final String catMapFileName = "catMapping.txt";
    
    private HashMap <String, String> catMap;

    private InputStreamReader getStream(){
        
    try {//Searching for catMapping.properties in the same folder as jar file
        String predecodedPath = CatMap.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String path = URLDecoder.decode(predecodedPath, "UTF-8");
        path = new File(path).getParent(); //removing name of the jar, leaving only path.
            
        File externalProps = new File(path + File.separator + catMapFileName);
            
        if (externalProps.exists()){
            System.out.println("catMapping taken from the jar's folder");
            return new InputStreamReader(new FileInputStream(externalProps));
        }
        
    } catch(Exception e){
            System.out.println(e.getMessage());
    }
        
    //reading properties from within the jar
    System.out.println("catMapping taken from within the jar");
    return new InputStreamReader(getClass().getResourceAsStream("/ImportDataFromPhone/" + catMapFileName ));
    }
        
    CatMap(){
        
        InputStreamReader input = getStream();
        BufferedReader reader = new BufferedReader(input);
        
        catMap = new HashMap(0); //Надо ли и тут указывать String,String
        String line;
        String category; 
        
        try{
        defaultCategory = reader.readLine();
        category = defaultCategory;
        
        while ((line=reader.readLine()) != null){
            
            if (line.matches("^\\s*$")) continue;
            if (line.matches("^\t.*$"))
                catMap.put(line.replaceFirst("\t", ""), category);
            else
                category = line;
        }
        } catch (Exception e){
            System.out.println(e.getMessage());
        }


        try {
            reader.close();
            input.close();
        } catch (Exception e){
            System.out.println(e.getMessage());
        }
        
        Iterator<String> i = new HashSet(catMap.keySet()).iterator();
        
    }
            
        
    
    public String getCat(String s){
        String item = s.trim();
        if (!catMap.isEmpty()){
            if (catMap.containsKey(item))
                return catMap.get(item);
            else
                return defaultCategory;
        } else {
            return defaultCategory;
        }
    }

    
}
